﻿using BusinessLayer;
using BusinessLayer.VDataEntities;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Http;
using TrialManagementTool.Helper;
using TrialManagementTool.Models;
using TrialManagmentTool.Helper;
using TrialManagmentTool.Models;

namespace TrialManagementTool.Controllers.API
{
    public class CommonController : ApiController
    {
        [HttpGet]
        public VDataEntity[] GetAllProvince()
        {
            VDataEntity[] result = BusinessLayer.LocationBusiness.GetProvinceList();
            return result;
        }

        public string AuthUser(User user)
        {
            if (ConfigurationManager.AppSettings["LDAPAuth"].ToString() == "true")
            {
                List<string> result = Common.authenticateUser("dow", user.username, user.password);
                if (result.Contains("已验证"))
                {
                    VDataEntity User = AdminBusiness.LoginByLDAP(user.username);
                    if (User != null)
                    {
                        HttpContext.Current.Session["AdminInfo"] = User;
                        UserHelper.SaveUserInfoToCookie(JsonHelper.GetJson<VDataEntity>(User));
                        LogHelper.Info(string.Format("Authentication user Request host name:{0},host ip:{1},url refer:{2}", HttpContext.Current.Request.UserHostName, HttpContext.Current.Request.UserHostAddress, HttpContext.Current.Request.UrlReferrer));
                        return "success";
                    }
                    else
                    {
                        return "当前用户不是管理员";
                    }

                }
                else
                {
                    return result[1];
                }
            }
            else
            {
                VDataEntity User = BusinessLayer.AdminBusiness.Login(user.username, user.password);
                if (User != null)
                {
                    HttpContext.Current.Session["AdminInfo"] = User;
                    UserHelper.SaveUserInfoToCookie(JsonHelper.GetJson<VDataEntity>(User));
                    //LogHelper.Info(string.Format("Authentication user Request host name:{0},host ip:{1},url refer:{2}", HttpContext.Current.Request.UserHostName, HttpContext.Current.Request.UserHostAddress, HttpContext.Current.Request.UrlReferrer));
                    return "success";
                }
                else
                {
                    return "验证失败,用户名或密码不正确";
                }
            }
        }

        public VDataEntity[] GetAdmins()
        {
            return AdminBusiness.GetAdmins();
        }

        [HttpPost]
        public string Timeout()
        {
            return "当前未登录或登录已超时";
        }

        public string AuthPromoterUser(User user)
        {
            VPromoter User = PromoterBusiness.ValidatePromoter(user.username);
            if (User != null)
            {
                HttpContext.Current.Session["PromoterInfo"] = User;

                UserHelper.SaveUserInfoToCookie(JsonHelper.GetJson<VPromoter>(User));
                return "success";
            }
            else
            {
                return "fail";
            }
        }

        public WeixinJSViewModel GetWeixinJs(string url)
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            WeixinJSViewModel weixinViewModel = new WeixinJSViewModel();
            weixinViewModel.timestamp = Convert.ToInt64(ts.TotalSeconds).ToString();

            weixinViewModel.noncestr = "weixinjs";

            weixinViewModel.ticket = new WeChatHelper().GetjsApiTicket();

            var jmdata = "jsapi_ticket={0}&noncestr={1}&timestamp={2}&url={3}";

            jmdata = string.Format(jmdata, weixinViewModel.ticket, weixinViewModel.noncestr, weixinViewModel.timestamp, url);
            weixinViewModel.appId = ConfigurationManager.AppSettings["CorpID"];
            weixinViewModel.signature = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(jmdata, "SHA1");
            weixinViewModel.IsDebug = ConfigurationManager.AppSettings["WeixinJSDebugMode"] == "true" ? true : false;
            weixinViewModel.url = url;
            return weixinViewModel;
        }

    }
}
